﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace V2
{
    public enum ChangeType {
        Add,
        Remove,
        // TODO: Update should probably not exist here. Keys should be considered immutable, instead we should Remove/Add and separate the idea of underlying value changing.
        Update,
        Clear
    };

    public class ChangeArgs {
        private readonly ChangeType m_ChangeType;
        private readonly IList< ChangeItem > m_Items;

        public ChangeArgs( ChangeType change_type, IList< ChangeItem > items ) {
            m_ChangeType = change_type;
            m_Items = items;
        }
        
        public ChangeArgs( ChangeType change_type, params ChangeItem[] items ) 
            : this( change_type, (IList<ChangeItem>)items ) {}

        public ChangeArgs( bool is_addition, Values[] items ) {
            List<ChangeItem> list = new List< ChangeItem >();
            for( int i=0 ; i < items.Length ; i++ ) {
                list.Add( new ChangeItem( is_addition? items[i] : null, !is_addition? items[i] : null ));
            }
            m_Items = list;
        }

        public ChangeType ChangeType { get { return m_ChangeType; } }

        public IEnumerable< ChangeItem > Items { get { return m_Items; } }

        public int Count { get { return m_Items.Count; } }

        public IEnumerable<Values> AdditionItems
        {
            get
            {
                foreach (ChangeItem item in m_Items) yield return item.Current;
            }
        }
        public IEnumerable<Values> RemovalItems
        {
            get
            {
                foreach (ChangeItem item in m_Items) yield return item.Previous;
            }
        }
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append( "Change{ " ).Append( ChangeType ).AppendLine(  );
            foreach (ChangeItem item in m_Items)
                sb.Append( "  " ).Append( item.Current ).Append( '{' ).Append( item.Previous ).Append( "}," ).AppendLine();
            sb.Append( " }" );
            return sb.ToString();
        }
    }


    public class ChangeItem{

        public readonly Values Current;
        public readonly Values Previous;

        public ChangeItem( Values current, Values previous ) {
            Current = current;
            Previous = previous;
        }
    }
}
